/**
 * @author 铭
 * @TODO 9/1/2024
 */
package com.woniuxy.controller;

import com.woniuxy.entitys.TbCustomer;
import com.woniuxy.entitys.TbUser;
import com.woniuxy.service.AuthCustomerService;
import com.woniuxy.service.AuthService;
import com.woniuxy.service.CustomerLoginService;
import com.woniuxy.service.UserLoginService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class LoginController {
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private CustomerLoginService customerLoginService;
    @Autowired
    private AuthService authService;
    @Autowired
    private AuthCustomerService authCustomerService;
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("auth/login")
    public ResultVO Login(@RequestBody Map<String,Object> map){
        String phone = (String) map.get("phone");
        String upwd = (String) map.get("upwd");
        String role = (String) map.get("role");

        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object o = valueOperations.get("mast:lock:" + phone);
        if (o != null && o.equals("lock")) {
            return ResultVO.failure("账户被锁定，请稍后再试");
        }

        if(Integer.parseInt(role) == 1){
            //顾客
            TbCustomer tbCustomer = new TbCustomer();
            tbCustomer.setPhone(phone);
            tbCustomer.setPassword(upwd);
            TbCustomer login = customerLoginService.login(tbCustomer);
            if (login!=null){
                String token = authCustomerService.login(login);
                redisTemplate.delete("mast:lock:" + phone);
                return ResultVO.success("顾客：" + phone + "，欢迎！",token);
            }else {
                Long increment = valueOperations.increment("mast:lock:" + phone, 1);
                if (increment == 6) {
                    valueOperations.set("mast:lock:" + phone, "lock", 60, TimeUnit.SECONDS);
                    return ResultVO.failure("账户已被锁定");
                }
                return ResultVO.failure("登录失败" + increment + "次");
            }


        }else {
            //工作人员
            TbUser user = new TbUser();
            user.setPhone(phone);
            user.setUpwd(upwd);
            TbUser login = userLoginService.login(user);
            if (login != null){
                String token = authService.login(login);
                redisTemplate.delete("mast:lock:" + phone);
                return ResultVO.success("工作人员：" + phone + "，欢迎！",token);
            }else {
                Long increment = valueOperations.increment("mast:lock:" + phone, 1);
                if (increment == 6) {
                    valueOperations.set("mast:lock:" + phone, "lock", 30, TimeUnit.SECONDS);
                    return ResultVO.failure("账户已被锁定");
                }
                return ResultVO.failure("登录失败" + increment + "次");
            }
        }

    }
    @DeleteMapping("/auth/user/logout")
    public ResultVO logoutUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        userLoginService.deleteToken(token);
        return ResultVO.success("安全退出成功");


    }
    @DeleteMapping("/auth/customer/logout")
    public ResultVO logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        customerLoginService.deleteToken(token);
        return ResultVO.success("安全退出成功");


    }
    @PostMapping("/auth/check")
    public ResultVO check(@RequestBody String token) {
        if (StringUtils.isEmpty(token)) {
            return ResultVO.failure("无效token");
        }
        if (!authService.isExit(token)) {
            if(!authCustomerService.isExit(token)){
                return ResultVO.failure("Token无效");
            }
        }
        return ResultVO.success("Token有效");

    }
    @PostMapping("auth/updPwd")
    public ResultVO updPwd(@RequestBody Map<Object,String> map){
        String oldPwd = map.get("oldPwd");
        String newPwd = map.get("newPwd");


        int i = userLoginService.updPwd(oldPwd,newPwd);
        return i>0?ResultVO.success(""):ResultVO.failure();

    }
    @PostMapping("auth/CusupdPwd")
    public ResultVO cupdPwd(@RequestBody Map<Object,String> map){
        String oldPwd = map.get("oldPwd");
        String newPwd = map.get("newPwd");


        int i = customerLoginService.cupdPwd(oldPwd,newPwd);
        return i>0?ResultVO.success(""):ResultVO.failure();

    }

}
